<?php

namespace App\Http\Controllers\Admin\Cashier;

use App\Models\Cashier\Apply;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cashier\Product;
use App\Models\Cashier\Category;
use App\Models\Cashier\Brand;
use App\Models\Cashier\Store;
use Excel;

class ProductController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_cashier' => 'active open',
            '_product' => 'active',
            'categories' => Category::get_categories(),
            'brands' => Brand::orderBy('sort_order')->get(),
            'stores' => Store::orderBy('id')->get(),
            'filter_categories' => Category::filter_categories(),
            'applies' => Apply::orderBy('sort_order')->get()
        ]);

    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $products = Product::all_products($request);
        return view('admin.cashier.product.index', compact('products'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $product = Product::find($id);
//        return $product;
        return view('admin.cashier.product.edit', compact('product'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|max:255',
            'stock' => 'required|integer',
            'sort_order' => 'required|integer|max:99',
            'description' => 'required'
        ]);

        $product = Product::find($id);
        $product->update($request->all());
        return redirect(route('cashier.product.index'))->with('success', '编辑商品成功~');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Product::destroy($id);
        return back()->with('success', '删除商品成功~');
    }


    /** ajax排序
     * @param Request $request
     */
    public function sort_order(Request $request)
    {
        $product = Product::find($request->id);
        $product->sort_order = $request->sort_order;
        $product->save();
    }


    /** ajax修改库存
     * @param Request $request
     */
    public function stock(Request $request)
    {
        $product = Product::find($request->id);
        $product->stock = $request->stock;
        $product->save();
    }

    /** 删除多条
     * @param Request $request
     */
    public function delete_more(Request $request)
    {
        $checked_id = $request->checked_id;
//        return $checked_id;

//        foreach ($checked_id as $id) {
        Product::destroy($checked_id);
//        }

    }


    /**
     * 盘点数据导出
     */
    public function export(Request $request)
    {
        $where = function ($query) use ($request) {
            //按照时间搜索
            if ($request->has('created_at') and $request->created_at != '') {
                $time = explode(" ~ ", $request->input('created_at'));
                $start_time = $time[0] . ' 00:00:00'; //这里单引号前面要打一空格
                $end_time = $time[1] . ' 23:59:59';
                $query->whereBetween('created_at', [$start_time, $end_time]);
            }

            //按门店
            if ($request->has('store_id') and $request->store_id != '-1') {
                $query->where('store_id', $request->store_id);
            }
        };

        $product_data = Product::with('store')->where($where)->get();
//        return $product_data;
        foreach ($product_data as $item) {
            $cellData[] = [
                [
                    '商品名称',
                    '单位',
                    '规格',
                    '单价',
                    '库存',
                    '生产厂家',
                    '门店'
                ],
                [
                    $item->name,
                    $item->unit,
                    $item->spec,
                    $item->price,
                    $item->stock,
                    $item->company,
                    $item->store['name']
                ],
            ];
//            return $cellData;
        }
//        exit;
        Excel::create('盘点商品', function ($excel) use ($cellData) {
            $excel->sheet('盘点商品数据', function ($sheet) use ($cellData) {
                $sheet->rows($cellData);
            });
        })->export('xls');
    }
}
